import requests
import json
import openpyxl

url = 'https://movie.douban.com/j/chart/top_list'
start = int(input('从第几部开始：'))
number = int(input('想你取多少：'))
headers = {
   'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'
}
params = {
'type': '5',
'interval_id':'100:90',
'action':'',
'start': start, #开始的位置起始位置为0
'limit': number #爬取的数量
}
response = requests.get(url=url,headers=headers,params=params)
print(response)
result = response.json()
# print(result)
moive_list = []
for each in result:
    # print(each)
    name =each['title']
    type =str(each['types'])
    time =str(each['release_date'])
    score = float(each['score'])
    information = [name, type, time, score]
    moive_list.append(information)
# 写入excle文件
wb = openpyxl.Workbook()
ws = wb.active
ws['A1'] = '名称'
ws['B1'] = '类型'
ws['C1'] = '上映时间'
ws['D1'] = '评分'
for movie in moive_list:
    ws.append(movie)
wb.save('./moive.xlsx')



    # 写入文本文件
    # with open("movie.txt",'a',encoding='utf-8') as f:
    #     f.write(information+"\n")
